package com.zoe.app.task;

import cn.hutool.core.date.DateUtil;
import com.zoe.app.controller.OrderController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author zoe.Zhang
 * @date 2022/12/20
 * @description
 */
@Component
@Slf4j
public class OrderJob {

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0/10 * * * * ?")
    public void autoCancel() {
        Set set = redisTemplate.opsForZSet().rangeByScore(OrderController.ORDER_SET, 0, DateUtil.current());
        log.info("自动取消任务扫描===>{}", set);
        for (Object o : set) {
            System.out.println("订单已经被取消===>" + o);
            Long remove = redisTemplate.opsForZSet().remove(OrderController.ORDER_SET, o);
        }

    }
}
